var QQMapWX = require('../../qqmap-wx-jssdk');
var qqmapsdk;
const app=getApp()
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    Uid:'',
    Uimage:'',
    likeUimage:'',
    myConcern: '',
    myFans:'',
    myYear: 3 ,
    barTitle:['作品','喜欢','宠物行程'],
    currentIndex:'',
    work:6,
    isliked:true,
    work:[],
    like:[]
  },

// 获取点赞数量
getMyFans:function(){
  let that = this;
  wx.request({   //that.data.Uid
    url: `${app.globalData.url}/users/`,
    header: { 
        'Content-Type': 'application/json'
    },
    data: {//这里写你要请求的参数
      Uid:wx.getStorageSync('Uid')
    },
  
    success: function(res) {
      console.log(res.data[0]);
      let atten=res.data[0][0].adsum>9999?parseInt(res.data[0][0].adsum/10000)+'w+':res.data[0][0].adsum
      that.setData({
        myFans:atten
      })
  
      
    }
  })
},

/* 获取关注数量 */
getConNumber:function(){
  let that = this;
  wx.request({   //that.data.Uid
    url: `${app.globalData.url}/users/`,
    header: { 
        'Content-Type': 'application/json'
    },
    data: {//这里写你要请求的参数
      Uid:wx.getStorageSync('Uid')
    },
  
    success: function(res) {
      let atten=res.data[1][0].atsum>9999?parseInt(res.data[1][0].atsum/10000)+'w+':res.data[1][0].atsum
      that.setData({
        myConcern:atten
      })
  
      
    }
  })
},

/*跳转关注  */
jumpMyConcern:function(){
  wx.navigateTo({
    url:'/pages/me-attenlist/me-attenlist?Uid='+wx.getStorageSync('Uid')+'&Uname='+wx.getStorageSync('Uname')
})
},
jumpMyFans:function(){
  wx.navigateTo({
    url:'/pages/me-fanlist/me-fanlist?Uid='+wx.getStorageSync('Uid')+'&Uname='+wx.getStorageSync('Uname')
})
},
  /* 取消喜欢 */
  deleteLikedWorks:function(e) {
    var that = this;
    var wid = e.target.dataset.id;
    wx.showModal({
      title:'提示',
      content:'确定要取消收藏吗？',
      showCancel: true, 
      cancelText: "取消", 
      cancelColor: '#888', 
      confirmText: "确定", 
      confirmColor: 'lightsalmon', 
      success: function(res){
        let Uid = wx.getStorageSync('Uid');
        if(res.confirm) {
          console.log('用户点击了确认');
          wx.request({   
            url: `${app.globalData.url}/users/my-like`,
            method:'DELETE',
            data: {
              Uid:Uid,
              Wid:wid
            },
          
            success: function(res) {
              that.getLikeData();
             
            }
          })
        }else if(res.cancel) {
          console.log('用户点击了取消');
        }
        
      }
      })
  },


  jumpCount:function(){  /* 跳转至个人详情页面 */
    wx.navigateTo({
      url: '/pages/my_account/my_account',
    })
  },

  bindPickerChange: function(e) {
  
    this.setData({
      index: e.detail.value
    })
  },
  deleteWorks:function(e){
    var that = this;
    var wid = e.target.dataset.id;
    wx.showModal({
      title:'提示',
      content:'确定要删除该作品吗？',
      showCancel: true, 
      cancelText: "取消", 
      cancelColor: '#888', 
      confirmText: "确定", 
      confirmColor: 'lightsalmon', 
      success: function(res){
        let Uid = wx.getStorageSync('Uid');
        if(res.confirm) {
          console.log('用户点击了确认删除');
          wx.request({   
            url: `${app.globalData.url}/users/my-works`,
            method:'DELETE',
            data: {
              Uid:Uid,
              Wid:wid
            },
          
            success: function(res) {
              that.importData();
             
            }
          })
        }else if(res.cancel) {
          console.log('用户点击了取消');
        }
        
      }
      })
    },
  
    //跳转至作品详情页
  jumpDetail: function (e) {
    console.log(e.currentTarget.id)
    let petsnum = e.currentTarget.id;
    wx.navigateTo({
      url:"/pages/com_detail/com_detail?Wid= "+petsnum
    })
  },
  
  onLoad: function (options) {
     // 实例化API核心类
     qqmapsdk = new QQMapWX({
      key: 'GNLBZ-ZYD66-FLGSF-MU5JJ-SMNVS-HUB3W'
    });
    let Uimage = wx.getStorageSync('Uimage');
    let Uid = wx.getStorageSync('Uid');
    this.setData({
      Uimage:Uimage,
      Uid:Uid
    })
    this.importData();/* 从数据库获取作品 */
    this.getConNumber();//获取关注数量
    this.getMyFans();//  获取点赞数量
    this.getLikeData()
  },
  onShow: function () {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
     key: 'GNLBZ-ZYD66-FLGSF-MU5JJ-SMNVS-HUB3W'
   });
   let Uimage = wx.getStorageSync('Uimage');
   let Uid = wx.getStorageSync('Uid');
   this.setData({
     Uimage:Uimage,
     Uid:Uid
   })
   this.importData();/* 从数据库获取作品 */
   this.getConNumber();//获取关注数量
   this.getMyFans();//  获取点赞数量
   this.getLikeData()
 },
  importData: function(){/* 查询数据 */
    let that = this;
    // console.log(this.data.Uid);
    wx.request({   
      url: `${app.globalData.url}/users/my-works`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        Uid:that.data.Uid
      },
    
      success: function(res) {
        console.log(res.data)
        let result = res.data;
        for (var index in result) {
          if(result[index].Wzan>9999){
            result[index].Wzan=parseInt(result[index].Wzan/10000)+'w+'
          }
        }
        if(result) {
          that.setData({
            work:result
          })
        }
       
      }
    })
  },
  getLikeData:function(){  /* 获取喜欢列表 */
    let that = this;
    wx.request({   
      url: `${app.globalData.url}/users/my-like`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        Uid:wx.getStorageSync('Uid')
      },
    
      success: function(res) {
        console.log(res.data)
        let result = res.data;
        for (var index in result) {
          if(result[index].Wzan>9999){
            result[index].Wzan=parseInt(result[index].Wzan/10000)+'w+'
          }
        }
        if(result) {
          that.setData({
            like:result,
            likeUimage:result.Uimage
          })  
        }
      }
    })
  },

  atuoGetLocation(e) {  //根据输入内容获取经纬度
    let that = this;
     // 调用接口
     qqmapsdk.geocoder({
      address: e.detail.value,
      complete: res => {
        console.log(res.result.location);   //经纬度对象
        var lat = res.result.location.lat;
        var lng = res.result.location.lng;
        wx.setStorageSync('lat', lat);
        wx.setStorageSync('lng', lng);

       
        wx.navigateTo({
          url: '/pages/my_petTrip1/my_petTrip1',
        })
      }
      });
  },
  /* 标签栏滑动 跳转 */
  changeCurrentIndex:function(e) {
    console.log(e);
    this.setData({
      currentIndex: e.currentTarget.id
    })
  },
  onTap: function (e) {
    //console.log(e);
  },
 
 
  /* 跳转至 home 页面*/
  jumpMyHome: function (options) {
    console.log(options);
    wx.navigateTo({
      url: '/pages/my_home/my_home',
    })
  },

  /* 跳转至 宠物卡片 */
  jumpMyPet: function (options) {
    wx.request({   /* 判断数据库中是否有数据 */
      url: `${app.globalData.url}/users/my-pet-publish`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        Uid:wx.getStorageSync('Uid')
      },
    
      success: function(res) {
        console.log(res.data);
        if(res.data!='') {
          console.log(res.data);
          wx.setStorageSync('Pid',res.data[0].Pid)
          wx.navigateTo({
                url: '/pages/my_pet_publish/my_pet_publish',
              })
        }else {
          console.log('数据库中没有数据------------填写信息');
          wx.navigateTo({
            url: '/pages/my_pet/my_pet',
          })
        }
        
    
      }
    })  
    
  },
  startInter : function(){
    var that = this;
    that.data.inter= setInterval(
        function () {
          that.importData();/* 从数据库获取作品 */
          that.getConNumber();//获取关注数量
          that.getMyFans();//  获取点赞数量
          this.getLikeData()
        }, 500);    
  },
  /**
   * 结束定时器
   */
  endInter: function(){
    var that = this;
    that.clearInterval(that.data.inter)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.endInter()
  }
})